<template>
  <div>
    <el-card>
      <el-row style="margin-bottom: 10px">
        <el-col :span="3" style="margin-right: 10px">
          <el-select v-model="selectReportStatus" placeholder="举报状态" value="">
            <el-option label="" value=""></el-option>
            <el-option label="未审核" value="0"></el-option>
            <el-option label="举报失败" value="1"></el-option>
            <el-option label="举报成功" value="2"></el-option>
          </el-select>
        </el-col>
        <el-col span="auto" style="margin-right: 10px">
          <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        </el-col>
        <el-col span="auto">
          <AddButton></AddButton>
        </el-col>
      </el-row>
      <el-table :data="tableData" border>
        <el-table-column label="ID" prop="id" width="70" header-align="center" align="center" sortable></el-table-column>
        <el-table-column label="举报人" prop="fromUserId" width="70" header-align="center" align="center">
          <template slot-scope="scope">
            <el-popover placement="right" trigger="click">
              <UserInfoCard :userId="scope.row.fromUserId" :key="scope.row.fromUserId"></UserInfoCard>
              <el-button size="mini" plain type="primary" slot="reference" @click="getUserId(scope.row.fromUserId)">{{
                scope.row.fromUserId }}</el-button>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="帖子" prop="postId" width="70" header-align="center" align="center">
        </el-table-column>
        <el-table-column label="举报原因" prop="reason" width="170" header-align="center" align="center">
        </el-table-column>
        <el-table-column label="举报状态" prop="reportStatus" width="100" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag :type="postReportType[scope.row.reportStatus]">{{
              postReportStatus[scope.row.reportStatus] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="描述" prop="description" width="170" header-align="center" align="center"></el-table-column>
        <el-table-column label="创建时间" prop="gmtCreate" width="170" header-align="center" align="center">
        </el-table-column>
        <el-table-column label="修改时间" prop="gmtModified" width="170" header-align="center" align="center">
        </el-table-column>
        <el-table-column label="操作" header-align="center" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleOpenEdit(scope.row)" style="margin-right: 10px"
              v-show="scope.row.reportStatus === 0">审核</el-button>
            <el-dialog title="审核举报信息" :visible.sync="dialogue" width="50%">
              <el-form>
                <el-form-item label="举报" prop="reportStatus">
                  <el-radio-group v-model="form.reportStatus">
                    <el-radio :label="1" border>失败</el-radio>
                    <el-radio :label="2" border>成功</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="举报结果描述" prop="description">
                  <el-input v-model="form.description" type="textarea" placeholder="请输入内容"
                    prefix-icon="el-icon-edit"></el-input>
                </el-form-item>
              </el-form>
              <span slot="footer" class="dialog-footer">
                <el-button @click="dialogue = false">取 消</el-button>
                <el-button type="primary" @click="handleEdit(scope.$index, scope.row)">确 定</el-button>
              </span>
            </el-dialog>
            <el-button size="mini" @click="deleteDialogOpen(scope.row)" type="danger">删除</el-button>
            <el-dialog title="确认删除" :visible.sync="deleteDialog" width="35%">
              <span slot="footer" class="dialog-footer">
                <el-button @click="deleteDialog = false">取 消</el-button>
                <el-button type="primary" @click="handleDelete(scope.$index, scope.row)">确 定</el-button>
              </span>
            </el-dialog>
          </template>
        </el-table-column>
      </el-table>
      <!-- 底部分页 -->
      <div style="text-align: center">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page.sync="currentPage" :page-sizes="[10, 15, 20]" :page-size="pageSize" :total="totalSize" background
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getReportList, editReportPost, deleteReportPost } from '@/api/post/report'
import UserInfoCard from '@/components/UserInfoCard.vue'
import jsCookie from 'js-cookie'

export default {
  components: {
    UserInfoCard
  },
  provide() {
    return {
      getDataList: this.getDataList,
    }
  },
  data() {
    return {
      dataList: [],
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      totalSize: 0,
      dialogue: false,
      deleteDialog: false,
      form: {
        id: 0,
        fromUserId: 0,
        postId: 0,
        reason: "",
        description: "",
        reportStatus: 0,
      },
      userId: 0,
      userDialogue: false,
      selectReportStatus: '',
      postReportType: ['info', 'danger', 'scucess'],
      postReportStatus: ['未审核', '举报失败', '举报成功'],
      toUserId: Number,
    }
  },
  methods: {
    getUserId(userId) {
      this.userDialogue = true
      this.userId = Number(userId)
    },
    init(data) {
      this.form = {
        id: data.id,
        fromUserId: data.fromUserId,
        postId: data.postId,
        reason: data.reason,
        toUserId: this.toUserId,
      }
    },
    handleOpenEdit(row) {
      this.dialogue = true
      this.init(row)
    },
    handleEdit(index, row) {
      editReportPost({
        "id": this.form.id,
        "description": this.form.description,
        "fromUserId": this.form.fromUserId,
        "postId": this.form.postId,
        "toUserId": this.toUserId,
        "reportStatus": this.form.reportStatus,
      }).then(res => {
        if (res.data.code === 200) {
          this.$message.success({
            message: res.data.message,
            duration: 1000
          })
          this.dialogue = false
          this.getDataList()
        } else {
          // this.$message.error({
          //   message: res.data.message,
          //   duration: 1000
          // })
        }
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getDataList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getDataList()
    },
    deleteDialogOpen(row) {
      this.deleteDialog = true
      this.init(row)
    },
    handleDelete(index, row) {
      deleteReportPost(this.form.id).then(res => {
        if (res.data.code === 200) {
          this.$message.success({
            message: res.data.message,
            duration: 1000
          })
          this.deleteDialog = false
          this.getDataList()
        } else {
          // this.$message.error({
          //   message: res.data.message,
          //   duration: 1000
          // })
        }
      })
    },
    getDataList() {
      getReportList({
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        reportStatus: this.selectReportStatus === '' ? undefined : Number(this.selectReportStatus),
      }).then(res => {
        if (res.data.code === 200) {
          this.dataList = res.data.pageResult.dataList
          this.tableData = this.dataList
          this.totalSize = res.data.pageResult.totalCount
        } else {
          // this.$message.error({
          //   message: res.data.message,
          //   duration: 1000
          // })
        }
      })
    },
    search() {
      getReportList({
        pageNo: 1,
        pageSize: this.pageSize,
        reportStatus: this.selectReportStatus === '' ? undefined : Number(this.selectReportStatus),
      }).then(res => {
        if (res.data.code === 200) {
          this.tableData = res.data.pageResult.dataList
          this.totalSize = res.data.pageResult.totalCount
        } else {
          // this.$message.error({
          //   message: res.data.message,
          //   duration: 1000
          // })
        }
      })
    },
  },
  created() {
    this.getDataList()
    this.toUserId = JSON.parse(jsCookie.get("user")).id
  }
}
</script>

<style lang="scss" scoped>
.el-pagination {
  margin-top: 20px;
}
</style>
